{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用numpy中的 linalg来进行svd分解\n",
    "svd分解： $$ Data_{m \\times n} = U_{m \\times m} \\Sigma_{m \\times n} V^T_{n \\times n} $$\n",
    "其中 $ U_{m \\times m} $ 中为左奇异向量，前 $ r $ 个即为所求， $ \\Sigma_{m \\times n} $ 中对角线上值为奇异值，其余值为0，一般对角线上的值按从大到小排序，且前 $ r $ 个奇异值之和占据整个奇异值之和的大部分，$ r $ 个之后的可置为0，视为噪声或冗余特征。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "U: [[-0.14142136 -0.98994949]\n",
      " [-0.98994949  0.14142136]]\n",
      "Sigma: [1.00000000e+01 2.82797782e-16]\n",
      "VT: [[-0.70710678 -0.70710678]\n",
      " [ 0.70710678 -0.70710678]]\n"
     ]
    }
   ],
   "source": [
    "U, Sigma, VT = np.linalg.svd([[1,1],[7,7]])\n",
    "print('U:', U)\n",
    "print('Sigma:', Sigma)\n",
    "print('VT:', VT)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def loadExData():\n",
    "    '''加载数据'''\n",
    "    return [[1,1,1,0,0],\n",
    "            [2,2,2,0,0],\n",
    "            [1,1,1,0,0],\n",
    "            [5,5,5,0,0],\n",
    "            [1,1,0,2,2],\n",
    "            [0,0,0,3,3],\n",
    "            [0,0,0,1,1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([9.72140007e+00, 5.29397912e+00, 6.84226362e-01, 4.11502614e-16,\n",
       "       1.36030206e-16])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Data = loadExData()\n",
    "U, Sigma, VT = np.linalg.svd(Data)\n",
    "Sigma"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以看到奇异值中前3个数值较大，而后两个太小。于是取前三个近似表示数据集矩阵 $ Data $:\n",
    "$$ Data_{m \\times n} \\approx U_{m \\times 3} \\Sigma_{3 \\times 3} V^T_{3 \\times r} $$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "尝试重构原始矩阵:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 1.00000000e+00,  1.00000000e+00,  1.00000000e+00,\n",
       "          7.75989921e-16,  7.71587483e-16],\n",
       "        [ 2.00000000e+00,  2.00000000e+00,  2.00000000e+00,\n",
       "          3.00514919e-16,  2.77832253e-16],\n",
       "        [ 1.00000000e+00,  1.00000000e+00,  1.00000000e+00,\n",
       "          2.18975112e-16,  2.07633779e-16],\n",
       "        [ 5.00000000e+00,  5.00000000e+00,  5.00000000e+00,\n",
       "          3.00675663e-17, -1.28697294e-17],\n",
       "        [ 1.00000000e+00,  1.00000000e+00, -5.48397422e-16,\n",
       "          2.00000000e+00,  2.00000000e+00],\n",
       "        [ 3.21319929e-16,  4.43562065e-16, -3.48967188e-16,\n",
       "          3.00000000e+00,  3.00000000e+00],\n",
       "        [ 9.71445147e-17,  1.45716772e-16, -1.52655666e-16,\n",
       "          1.00000000e+00,  1.00000000e+00]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Sig3 = np.mat([[Sigma[0],0,0],[0,Sigma[1],0],[0,0,Sigma[2]]])\n",
    "Data_s = U[:,:3] * Sig3 * VT[:3,:]\n",
    "Data_s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 小例子：基于协同过滤的推荐引擎\n",
    "利用用户的评价来进行物品之间相似度的计算。<br>\n",
    "评价矩阵: $$ \\begin{bmatrix} x_1^{(1)} & x_2^{(1)} & \\cdots & x_n^{(1)} \\\\ x_1^{(2)} & x_2^{(2)} & \\cdots & x_n^{(2)} \\\\ \\vdots & \\vdots & \\ddots & \\vdots \\\\ x_1^{(m)} & x_2^{(m)} & \\cdots & x_n^{(m)} \\\\ \\end{bmatrix}$$\n",
    "其中 $ m $ 表示用户数， $ n $ 表示物品数\n",
    "（1）欧氏距离(物品 $ x_i 与 x_j $ )\n",
    "$$ dist = \\sqrt{(x_i^{(1)} - x_j^{(1)})^2 + x_i^{(2)} - x_j^{(2)})^2 + ... + x_i^{(m)} - x_j^{(m)})^2} $$ \n",
    "（2）皮尔逊相关系数\n",
    "使用numpy中的numpy.corrcoef()来计算，由于皮尔逊系数的取值在 $ [-1,1] $ 之间，因此用 $ 0.5 + 0.5*np.corrcoef() $ 将值映射到 $ [0,1] $\n",
    "（3）余弦相似度\n",
    "$$ \\cos \\theta = \\frac{A \\cdot B}{|| A || || B ||} $$\n",
    "余弦相似度取值范围也是 $ [-1,1] $ ，因此使用2范数将值映射到 $ [0,1] $ 。 $ 向量[4,2,2]的 2范数=\\sqrt{4^2 + 2^2 + 2^2} $"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "def euclidSim(inA, inB):\n",
    "    '''欧氏距离'''\n",
    "    return 1.0 / (1.0 + np.linalg.norm(inA - inB))\n",
    "\n",
    "def pearsSim(inA, inB):\n",
    "    '''皮尔逊相关系数'''\n",
    "    # 二维或一维情况下，点A与点B可以确定一条直线\n",
    "    if len(inA) < 3 : return 1.0\n",
    "    return 0.5 + 0.5 * np.corrcoef(inA, inB, rowvar = 0)[0][1]\n",
    "\n",
    "def cosSim(inA, inB):\n",
    "    '''余弦相似度'''\n",
    "    num = np.sum(inA.T * inB)\n",
    "    denom = np.linalg.norm(inA) * np.linalg.norm(inB)\n",
    "    return 0.5 + 0.5 * (num / denom)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第0个变量与第0个变量的欧氏距离： 1.0\n",
      "第0个变量与第4个变量的欧氏距离： 0.13367660240019172\n",
      "第0个变量与第0个变量的皮尔逊相关系数： 1.0\n",
      "第0个变量与第4个变量的皮尔逊相关系数： 0.23768619407595815\n",
      "第0个变量与第0个变量的余弦相似度： 0.9999999999999999\n",
      "第0个变量与第4个变量的余弦相似度： 0.5472455591261534\n"
     ]
    }
   ],
   "source": [
    "DataMat = np.mat(loadExData())\n",
    "print(\"第0个变量与第0个变量的欧氏距离：\", euclidSim(DataMat[:,0], DataMat[:,0]))\n",
    "print(\"第0个变量与第4个变量的欧氏距离：\", euclidSim(DataMat[:,0], DataMat[:,4]))\n",
    "print(\"第0个变量与第0个变量的皮尔逊相关系数：\", pearsSim(DataMat[:,0], DataMat[:,0]))\n",
    "print(\"第0个变量与第4个变量的皮尔逊相关系数：\", pearsSim(DataMat[:,0], DataMat[:,4]))\n",
    "print(\"第0个变量与第0个变量的余弦相似度：\", cosSim(DataMat[:,0], DataMat[:,0]))\n",
    "print(\"第0个变量与第4个变量的余弦相似度：\", cosSim(DataMat[:,0], DataMat[:,4]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "def standEst(dataMat, user, simMeas, item):\n",
    "    '''估计评分值（使用其他用户已经评价过的物品与待预测item进行相似度计算）\n",
    "    Args:\n",
    "        dataMat: 数据集\n",
    "        user: 待预测用户\n",
    "        simMeas: 相似度计算方法\n",
    "        item: 待预测评分的物品\n",
    "    Return:\n",
    "        某个物品的预测评分\n",
    "    '''\n",
    "    n = np.shape(dataMat)[1]\n",
    "    simTotal = 0.0\n",
    "    ratSimTotal = 0.0\n",
    "    for j in range(n):\n",
    "        userRating = dataMat[user,j]\n",
    "        if userRating == 0:\n",
    "            continue\n",
    "        # 寻找两个用户都评价过的物品\n",
    "        overlap = np.nonzero(np.logical_and(dataMat[:,item].A>0, dataMat[:,j].A>0))[0]\n",
    "        if len(overlap) == 0: similarity = 0\n",
    "        else: similarity = simMeas(dataMat[overlap, item], dataMat[overlap, j])\n",
    "        print('the %d and %d similarity is: %f' % (item, j, similarity))\n",
    "        simTotal += similarity\n",
    "        ratSimTotal += similarity * userRating\n",
    "    if simTotal == 0: return 0\n",
    "    else: return ratSimTotal / simTotal\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "def recommend(dataMat, user, N=3, simMeas=cosSim, estMethod=standEst):\n",
    "    '''推荐引擎'''\n",
    "    # 找到未评级的物品\n",
    "    unratedItems = np.nonzero(dataMat[user,:].A==0)[1]\n",
    "    if len(unratedItems) == 0: return 'you rated everything'\n",
    "    itemScores = []\n",
    "    for item in unratedItems:\n",
    "        estimatedScore = estMethod(dataMat, user, simMeas, item)\n",
    "        itemScores.append((item, estimatedScore))\n",
    "    return sorted(itemScores, key=lambda jj:jj[1], reverse=True)[:N]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[4, 4, 0, 2, 2],\n",
       "        [4, 0, 0, 3, 3],\n",
       "        [4, 0, 0, 1, 1],\n",
       "        [1, 1, 1, 2, 0],\n",
       "        [2, 2, 2, 0, 0],\n",
       "        [1, 1, 1, 0, 0],\n",
       "        [5, 5, 5, 0, 0]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "myMat = [[4,4,0,2,2],\n",
    "            [4,0,0,3,3],\n",
    "            [4,0,0,1,1],\n",
    "            [1,1,1,2,0],\n",
    "            [2,2,2,0,0],\n",
    "            [1,1,1,0,0],\n",
    "            [5,5,5,0,0]]\n",
    "myMat = np.mat(myMat)\n",
    "myMat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the 1 and 0 similarity is: 1.000000\n",
      "the 1 and 3 similarity is: 0.928746\n",
      "the 1 and 4 similarity is: 1.000000\n",
      "the 2 and 0 similarity is: 1.000000\n",
      "the 2 and 3 similarity is: 1.000000\n",
      "the 2 and 4 similarity is: 0.000000\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[(2, 2.5), (1, 2.0243290220056256)]"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "recommend(myMat, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "def loadExData2():\n",
    "    return[[0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 5],\n",
    "           [0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 3],\n",
    "           [0, 0, 0, 0, 4, 0, 0, 1, 0, 4, 0],\n",
    "           [3, 3, 4, 0, 0, 0, 0, 2, 2, 0, 0],\n",
    "           [5, 4, 5, 0, 0, 0, 0, 5, 5, 0, 0],\n",
    "           [0, 0, 0, 0, 5, 0, 1, 0, 0, 5, 0],\n",
    "           [4, 3, 4, 0, 0, 0, 0, 5, 5, 0, 1],\n",
    "           [0, 0, 0, 4, 0, 4, 0, 0, 0, 0, 4],\n",
    "           [0, 0, 0, 2, 0, 2, 5, 0, 0, 1, 2],\n",
    "           [0, 0, 0, 0, 5, 0, 0, 0, 0, 4, 0],\n",
    "           [1, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "U,Sigma,VT=np.linalg.svd(np.mat(loadExData2()))\n",
    "myMat=np.mat(loadExData2())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sigma平方和: 541.9999999999995\n",
      "Sigma平方和的90%: 487.7999999999996\n",
      "S前两个Sigma和: 378.8295595113579\n",
      "S前三个Sigma和: 500.5002891275793\n"
     ]
    }
   ],
   "source": [
    "Sig2=Sigma**2\n",
    "sum_Sig2 = np.sum(Sig2)\n",
    "print('Sigma平方和:',sum_Sig2)\n",
    "sumSig29 = np.sum(Sig2)*0.9\n",
    "print('Sigma平方和的90%:',sumSig29)\n",
    "print('S前两个Sigma和:',np.sum(Sig2[:2]))\n",
    "print('S前三个Sigma和:',np.sum(Sig2[:3]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "def svdEst(dataMat, user, simMeas, item):\n",
    "    '''基于SVD的评分估计'''\n",
    "    n = np.shape(dataMat)[1]\n",
    "    simTotal = 0.0; ratSimTotal = 0.0\n",
    "    U,Sigma,VT = np.linalg.svd(dataMat)\n",
    "    # 建立对角矩阵\n",
    "    Sig4 = np.mat(np.eye(4)*Sigma[:4]) \n",
    "    # 构建转换后的物品\n",
    "    xformedItems = dataMat.T * U[:,:4] * Sig4.I  \n",
    "    for j in range(n):\n",
    "        userRating = dataMat[user,j]\n",
    "        if userRating == 0 or j==item: continue\n",
    "        similarity = simMeas(xformedItems[item,:].T,\\\n",
    "                             xformedItems[j,:].T)\n",
    "        print('the %d and %d similarity is: %f' % (item, j, similarity))\n",
    "        simTotal += similarity\n",
    "        ratSimTotal += similarity * userRating\n",
    "    if simTotal == 0: return 0\n",
    "    else: return ratSimTotal/simTotal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the 0 and 3 similarity is: 0.490950\n",
      "the 0 and 5 similarity is: 0.484274\n",
      "the 0 and 10 similarity is: 0.512755\n",
      "the 1 and 3 similarity is: 0.491294\n",
      "the 1 and 5 similarity is: 0.481516\n",
      "the 1 and 10 similarity is: 0.509709\n",
      "the 2 and 3 similarity is: 0.491573\n",
      "the 2 and 5 similarity is: 0.482346\n",
      "the 2 and 10 similarity is: 0.510584\n",
      "the 4 and 3 similarity is: 0.450495\n",
      "the 4 and 5 similarity is: 0.506795\n",
      "the 4 and 10 similarity is: 0.512896\n",
      "the 6 and 3 similarity is: 0.743699\n",
      "the 6 and 5 similarity is: 0.468366\n",
      "the 6 and 10 similarity is: 0.439465\n",
      "the 7 and 3 similarity is: 0.482175\n",
      "the 7 and 5 similarity is: 0.494716\n",
      "the 7 and 10 similarity is: 0.524970\n",
      "the 8 and 3 similarity is: 0.491307\n",
      "the 8 and 5 similarity is: 0.491228\n",
      "the 8 and 10 similarity is: 0.520290\n",
      "the 9 and 3 similarity is: 0.522379\n",
      "the 9 and 5 similarity is: 0.496130\n",
      "the 9 and 10 similarity is: 0.493617\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[(4, 3.3447149384692283), (7, 3.329402072452697), (9, 3.328100876390069)]"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "recommend(myMat, 1, estMethod=svdEst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the 0 and 3 similarity is: 0.341942\n",
      "the 0 and 5 similarity is: 0.124132\n",
      "the 0 and 10 similarity is: 0.116698\n",
      "the 1 and 3 similarity is: 0.345560\n",
      "the 1 and 5 similarity is: 0.126456\n",
      "the 1 and 10 similarity is: 0.118892\n",
      "the 2 and 3 similarity is: 0.345149\n",
      "the 2 and 5 similarity is: 0.126190\n",
      "the 2 and 10 similarity is: 0.118640\n",
      "the 4 and 3 similarity is: 0.450126\n",
      "the 4 and 5 similarity is: 0.528504\n",
      "the 4 and 10 similarity is: 0.544647\n",
      "the 6 and 3 similarity is: 0.923822\n",
      "the 6 and 5 similarity is: 0.724840\n",
      "the 6 and 10 similarity is: 0.710896\n",
      "the 7 and 3 similarity is: 0.319482\n",
      "the 7 and 5 similarity is: 0.118324\n",
      "the 7 and 10 similarity is: 0.113370\n",
      "the 8 and 3 similarity is: 0.334910\n",
      "the 8 and 5 similarity is: 0.119673\n",
      "the 8 and 10 similarity is: 0.112497\n",
      "the 9 and 3 similarity is: 0.566918\n",
      "the 9 and 5 similarity is: 0.590049\n",
      "the 9 and 10 similarity is: 0.602380\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[(4, 3.3469521867021736), (9, 3.3353796573274703), (6, 3.307193027813037)]"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "recommend(myMat, 1, estMethod=svdEst, simMeas=pearsSim)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基于SVD图像的压缩"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "def printMat(inMat, thresh=0.8):\n",
    "    for i in range(32):\n",
    "        for k in range(32):\n",
    "            if float(inMat[i,k]) > thresh:\n",
    "                print(1, end=''),\n",
    "            else: print(0, end=''),\n",
    "        print('') \n",
    "\n",
    "def imgCompress(numSV=3, thresh=0.8):\n",
    "    myl = []\n",
    "    for line in open('0_5.txt').readlines():\n",
    "        newRow = []\n",
    "        for i in range(32):\n",
    "            newRow.append(int(line[i]))\n",
    "        myl.append(newRow)\n",
    "    myMat = np.mat(myl)\n",
    "    print(\"****original matrix******\")\n",
    "    printMat(myMat, thresh)\n",
    "    U,Sigma,VT = np.linalg.svd(myMat)\n",
    "    SigRecon = np.mat(np.zeros((numSV, numSV)))\n",
    "    for k in range(numSV):#construct diagonal matrix from vector\n",
    "        SigRecon[k,k] = Sigma[k]\n",
    "    reconMat = U[:,:numSV]*SigRecon*VT[:numSV,:]\n",
    "    print(\"****reconstructed matrix using %d singular values******\" % numSV)\n",
    "    printMat(reconMat, thresh)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "****original matrix******\n",
      "00000000000000110000000000000000\n",
      "00000000000011111100000000000000\n",
      "00000000000111111110000000000000\n",
      "00000000001111111111000000000000\n",
      "00000000111111111111100000000000\n",
      "00000001111111111111110000000000\n",
      "00000000111111111111111000000000\n",
      "00000000111111100001111100000000\n",
      "00000001111111000001111100000000\n",
      "00000011111100000000111100000000\n",
      "00000011111100000000111110000000\n",
      "00000011111100000000011110000000\n",
      "00000011111100000000011110000000\n",
      "00000001111110000000001111000000\n",
      "00000011111110000000001111000000\n",
      "00000011111100000000001111000000\n",
      "00000001111100000000001111000000\n",
      "00000011111100000000001111000000\n",
      "00000001111100000000001111000000\n",
      "00000001111100000000011111000000\n",
      "00000000111110000000001111100000\n",
      "00000000111110000000001111100000\n",
      "00000000111110000000001111100000\n",
      "00000000111110000000011111000000\n",
      "00000000111110000000111111000000\n",
      "00000000111111000001111110000000\n",
      "00000000011111111111111110000000\n",
      "00000000001111111111111110000000\n",
      "00000000001111111111111110000000\n",
      "00000000000111111111111000000000\n",
      "00000000000011111111110000000000\n",
      "00000000000000111111000000000000\n",
      "****reconstructed matrix using 2 singular values******\n",
      "00000000000000000000000000000000\n",
      "00000000000000000000000000000000\n",
      "00000000000001111100000000000000\n",
      "00000000000011111111000000000000\n",
      "00000000000111111111100000000000\n",
      "00000000001111111111110000000000\n",
      "00000000001111111111110000000000\n",
      "00000000011110000000001000000000\n",
      "00000000111100000000001100000000\n",
      "00000000111100000000001110000000\n",
      "00000000111100000000001110000000\n",
      "00000000111100000000001110000000\n",
      "00000000111100000000001110000000\n",
      "00000000111100000000001110000000\n",
      "00000000111100000000001110000000\n",
      "00000000111100000000001110000000\n",
      "00000000111100000000001110000000\n",
      "00000000111100000000001110000000\n",
      "00000000111100000000001110000000\n",
      "00000000111100000000001110000000\n",
      "00000000111100000000001110000000\n",
      "00000000111100000000001110000000\n",
      "00000000111100000000001110000000\n",
      "00000000111100000000001110000000\n",
      "00000000111100000000001110000000\n",
      "00000000111100000000001100000000\n",
      "00000000001111111111111000000000\n",
      "00000000001111111111110000000000\n",
      "00000000001111111111110000000000\n",
      "00000000000011111111100000000000\n",
      "00000000000011111111000000000000\n",
      "00000000000000000000000000000000\n"
     ]
    }
   ],
   "source": [
    "imgCompress(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
